<?php
namespace Home\Controller;
use Think\Controller;
class FaqiController extends Controller {

    public function one()
    {
        $xm_id = I('get.xm_id');
        $this->assign('xm_id',$xm_id);

        $this->display();
    }
    //个人 学校 gs cs
    public function index()
    {
        $fq_sf = I('get.fq_sf');$this->assign('fq_sf',$fq_sf);
        $xm_id = I('get.xm_id');$this->assign('xm_id',$xm_id);
        //用户信息
        $userinfo = M('gy_user')->where(['id'=>$_SESSION['id']])->find();
        $this->assign('userinfo',$userinfo);
        //项目信息
        $xminfo = M('gy_xm')->where(['id'=>$xm_id])->find();
        $this->assign('xminfo',$xminfo);
        $this->display(); 
    }

    //发起
    public function fqfx()
    {
        if (IS_POST) {
            $data['xm_id'] = I('post.xm_id');
            $data['fq_sf'] = I('post.fq_sf');
            $data['uid'] = $_SESSION['id'];
            $data['name'] = $_SESSION['name'];
            $data['mubiao'] = I('post.mubiao') == ''?'0':I('post.mubiao');
            $data['msg'] = I('post.msg');
            $data['time'] = time();

            //检测目标是否为整数
            if (!ctype_digit($data['mubiao'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '非法目标'
                );
                echo json_encode($arr);
                exit;
            }
            if (empty($data['msg'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入一个口号'
                );
                echo json_encode($arr);
                exit;
            }

            $data['school'] = I('post.school');
            //学校
            if ($data['fq_sf'] == 2) {
                if (empty($data['school'])) {
                    $arr = array(
                        'error' => 0,
                        'info' => '请输入学校名称'
                    );
                    echo json_encode($arr);
                    exit;
                }
                //检测
                $checkfq = M('gy_faqi')->where(['xm_id'=>$data['xm_id'],'uid'=>$_SESSION['id']])->find();
                if ($checkfq) {
                    //存在更新
                    M('gy_faqi')->where(['id'=>$checkfq['id']])->save($data);
                    $res = $checkfq['id'];
                }else {
                    //入库 
                    $res = M('gy_faqi')->add($data);
                }
                $url = U('faqi/fqfx',array('id'=>$res));
                $arr = array(
                    'error' => 1,
                    'info' => '成功',
                    'url' => $url
                    );
                echo json_encode($arr);
            //公司
            }elseif ($data['fq_sf'] == 3) {
                if (empty($data['school'])) {
                    $arr = array(
                        'error' => 0,
                        'info' => '请输入公司名称'
                    );
                    echo json_encode($arr);
                    exit;
                }
                //检测
                $checkfq = M('gy_faqi')->where(['xm_id'=>$data['xm_id'],'uid'=>$_SESSION['id']])->find();
                if ($checkfq) {
                    //存在更新
                    M('gy_faqi')->where(['id'=>$checkfq['id']])->save($data);
                    $res = $checkfq['id'];
                }else {
                    //入库 
                    $res = M('gy_faqi')->add($data);
                }
                $url = U('faqi/fqfx',array('id'=>$res));
                $arr = array(
                    'error' => 1,
                    'info' => '成功',
                    'url' => $url
                    );
                echo json_encode($arr);
            //慈善
            }elseif ($data['fq_sf'] == 4) {
                if (empty($data['school'])) {
                    $arr = array(
                        'error' => 0,
                        'info' => '请输入慈善组织名称'
                    );
                    echo json_encode($arr);
                    exit;
                }
                //检测
                $checkfq = M('gy_faqi')->where(['xm_id'=>$data['xm_id'],'uid'=>$_SESSION['id']])->find();
                if ($checkfq) {
                    //存在更新
                    M('gy_faqi')->where(['id'=>$checkfq['id']])->save($data);
                    $res = $checkfq['id'];
                }else {
                    //入库 
                    $res = M('gy_faqi')->add($data);
                }
                $url = U('faqi/fqfx',array('id'=>$res));
                $arr = array(
                    'error' => 1,
                    'info' => '成功',
                    'url' => $url
                    );
                echo json_encode($arr);
            }else {
                //检测该项目是否有发起过
                $checkfq = M('gy_faqi')->where(['xm_id'=>$data['xm_id'],'uid'=>$_SESSION['id']])->find();
                if ($checkfq) {
                    $url = U('index/xm',array('id'=>$data['xm_id']));
                    $arr = array(
                        'error' => 0,
                        'info' => '你已发起过此项目',
                        'url' => $url
                        );
                    echo json_encode($arr);
                    exit;
                }

                //入库 
                $res = M('gy_faqi')->add($data);
                 if ($res) {
                    $url = U('faqi/fqfx',array('id'=>$res));
                    $arr = array(
                        'error' => 1,
                        'info' => '成功',
                        'url' => $url
                        );
                    echo json_encode($arr);
                 }
            }

        }else {
            $id = I('get.id');
            $this->assign('id',$id);

            $sql = "select t1.*,t2.name,t2.face from gy_faqi as t1 inner join gy_user as t2 on t1.uid=t2.id where t1.id='{$id}'";
            $faqiinfo = M()->query($sql);
            $faqiinfo = current($faqiinfo);
            $this->assign('faqiinfo',$faqiinfo);

            //项目信息
            $xminfo = M('gy_xm')->where(['id'=>$faqiinfo['xm_id']])->find();
            $this->assign('xminfo',$xminfo);

            $this->display();
        }
    }
    //图片
    public function imgs()
    {
        $upload = new \Think\Upload();// 实例化上传类    
        $upload->maxSize   =     3145728 ;// 设置附件上传大小    
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
        $upload->savePath  =      'Uploads/imgs/'; // 设置附件上传目录    
        // 上传文件     
        $info   =   $upload->upload();    
        if(!$info) {// 上传错误提示错误信息         
            $arr = array(
            'code' => 0,
            'msg' => $upload->getError(),
            );
            echo json_encode($arr);
        }else{// 上传成功   
            $info = current($info);//减维 
            //绝对路径
            $url =  __ROOT__.'/'.'Uploads/'.$info['savepath'].$info['savename'];   
            //相对路径
            $path = '/'.'Uploads/'.$info['savepath'].$info['savename'];

            //判断是否有认证信息
            $res_info = M('gy_faqi')->where(array('xm_id'=>I('get.xm_id'),'uid'=>$_SESSION['id']))->find();
            if ($res_info) {
                //删除旧图片
                $oldpath = M('gy_faqi')->where(array('xm_id'=>I('get.xm_id'),'uid'=>$_SESSION['id']))->getField('imgs');
                unlink('./'.$oldpath);
                //入库
                M('gy_faqi')->where(array('xm_id'=>I('get.xm_id'),'uid'=>$_SESSION['id']))->setField('imgs',$path);
            }else {
                //新添加
                $data['xm_id'] = I('get.xm_id');
                $data['uid'] = $_SESSION['id'];
                $data['imgs'] = $path;
                $data['time'] = time();
                M('gy_faqi')->add($data);
            }

            $arr = array(
            'code' => 0,
            'msg' => '上传成功',
            'data'=> array(
                'src' => $url,
                'title' => $info['savename']
                )
            );
            echo json_encode($arr);
        }
    }
    //编辑
    public function edit()
    {
        if (IS_POST) {
            $id = I('post.id');
            $fq_sf = I('post.fq_sf');
            $data['msg'] = I('post.msg');
            $data['mubiao'] = I('post.mubiao') == ''?'0':I('post.mubiao');

            //检测目标是否为整数
            if (!ctype_digit($data['mubiao'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '目标不能为空'
                );
                echo json_encode($arr);
                exit;
            }
            if (empty($data['msg'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '口号不能为空'
                );
                echo json_encode($arr);
                exit;
            }
            $data['school'] = I('post.school');
            //学校
            if ($fq_sf == 2) {
                if (empty($data['school'])) {
                    $arr = array(
                        'error' => 0,
                        'info' => '请输入学校名称'
                    );
                    echo json_encode($arr);
                    exit;
                }
            //公司
            }elseif ($fq_sf == 3) {
                if (empty($data['school'])) {
                    $arr = array(
                        'error' => 0,
                        'info' => '请输入公司名称'
                    );
                    echo json_encode($arr);
                    exit;
                }
            //慈善
            }elseif ($fq_sf == 4) {
                if (empty($data['school'])) {
                    $arr = array(
                        'error' => 0,
                        'info' => '请输入慈善组织名称'
                    );
                    echo json_encode($arr);
                    exit;
                }
            }else {
            }
            $res = M('gy_faqi')->where(array('id'=>$id))->setField($data);
            if ($res) {
                $url = U('faqi/fqfx',array('id'=>$id));
                $arr = array(
                    'error' => 1 ,
                    'info' => '更新成功',
                    'url' => $url
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0 ,
                    'info' => '更新失败'
                    );
                echo json_encode($arr);
            }

        }else {

         $id = I('get.id');
         $this->assign('id',$id);

         $sql = "select t1.*,t2.name,t2.face from gy_faqi as t1 inner join gy_user as t2 on t1.uid=t2.id where t1.id='{$id}'";
         $faqiinfo = M()->query($sql);
         $faqiinfo = current($faqiinfo);
         $this->assign('faqiinfo',$faqiinfo);
         //项目信息
         $xminfo = M('gy_xm')->where(['id'=>$faqiinfo['xm_id']])->find();
         $this->assign('xminfo',$xminfo);

         $this->display();

        }
    }
    //编辑图片
    public function editimgs()
    {
        $upload = new \Think\Upload();// 实例化上传类    
        $upload->maxSize   =     3145728 ;// 设置附件上传大小    
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
        $upload->savePath  =      'Uploads/imgs/'; // 设置附件上传目录    
        // 上传文件     
        $info   =   $upload->upload();    
        if(!$info) {// 上传错误提示错误信息         
            $arr = array(
            'code' => 0,
            'msg' => $upload->getError(),
            );
            echo json_encode($arr);
        }else{// 上传成功   
            $info = current($info);//减维 
            //绝对路径
            $url =  __ROOT__.'/'.'Uploads/'.$info['savepath'].$info['savename'];   
            //相对路径
            $path = '/'.'Uploads/'.$info['savepath'].$info['savename'];

            //判断是否有认证信息
            $res_info = M('gy_faqi')->where(array('id'=>I('get.id')))->find();
            if ($res_info) {
                //删除旧图片
                $oldpath = $res_info['imgs'];
                unlink('./'.$oldpath);
                //入库
                M('gy_faqi')->where(array('id'=>I('get.id')))->setField('imgs',$path);
            }
            $arr = array(
            'code' => 0,
            'msg' => '上传成功',
            'data'=> array(
                'src' => $url,
                'title' => $info['savename']
                )
            );
            echo json_encode($arr);
        }
    }













}